package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.domain.NewsContent;
import cn.wolfcode.trip.base.domain.NewsType;
import cn.wolfcode.trip.base.query.NewsQueryObject;
import cn.wolfcode.trip.base.service.INewsService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.util.List;

@Controller
@RequestMapping("news")
public class NewsController {

    @Autowired
    private INewsService newsService;
    @Autowired
    private ServletContext servletContext;

    /**
     * 待审核日报页面
     *
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") NewsQueryObject qo) {
        qo.setState(0);
        qo.setOrderBy("n.createtime desc");
        model.addAttribute("result", newsService.queryForList(qo));
        return "news/list";
    }

    /**
     * 更改草稿日报为发布日报状态
     *
     * @param news
     * @return
     */
    @RequestMapping("changeState")
    @ResponseBody
    public JSONResult changeState(News news) {
        JSONResult result = new JSONResult();
        try {
            newsService.changeState(news);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 删除日报
     *
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Long id) {
        JSONResult result = new JSONResult();
        try {
            newsService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 获取所有日报类型
     *
     * @return
     */
    @RequestMapping("typeList")
    @ResponseBody
    public List<NewsType> typeList() {
        return newsService.typeList();
    }

    /**
     * 获取日报内容
     *
     * @param id
     * @return
     */
    @RequestMapping("getContentById")
    @ResponseBody
    public NewsContent getContentById(Long id) {
        return newsService.getContentById(id);
    }

    /**
     * 保存/更新日报
     * @param file
     * @param news
     * @return
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(MultipartFile file, News news) {
        JSONResult result = new JSONResult();
        try {
            if (file != null && file.getSize() > 0) {
                String imgUrl = UploadUtil.upload(file, servletContext.getRealPath("/upload"));
                news.setCoverUrl(imgUrl);
            }
            if (news.getId() == null) {
                newsService.save(news);
            } else {
                newsService.update(news);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

}
